package com.neusoft.elmboot.service.impl;

import com.neusoft.elmboot.dto.FoodReqDto;
import com.neusoft.elmboot.mapper.FoodMapper;
import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.service.FoodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FoodServiceImpl implements FoodService {

    @Autowired
    private FoodMapper foodMapper;

    @Override
    public List<Food> getFoodsList(FoodReqDto params) {
        // 模拟数据库查询出的食品数据。
//        Food f1 = new Food(1,"肯德基","肯德基好吃","",10.0,1,"不错");
//        Food f2 = new Food(2,"包子","包子好吃","",1.0,2,"包子不错");
        List<Food> foodList = new ArrayList<>();
//        foodList.add(f1);
//        foodList.add(f2);

        Map<String,Object> pa = new HashMap<>();
        // 食品相关的查询参数
        pa.put("foodId",params.getFoodId());
        pa.put("foodName",null == params.getFoodName() ? null :
                params.getFoodName().trim());
        // 分页相关的参数
        pa.put("beginIndex",params.getBeginIndex());
        pa.put("pageSize",params.getLimit());

        foodList = foodMapper.listAllFood(pa);

        return foodList;
    }

    @Override
    public Long getFoodsCount(FoodReqDto params) {
        Map<String,Object> pa = new HashMap<>();
        // 食品相关的查询参数
        pa.put("foodId",params.getFoodId());
        pa.put("foodName",null == params.getFoodName() ? null :
                params.getFoodName().trim());
        return foodMapper.listAllFoodCount(pa);
    }

    @Override
    public Integer addFood(FoodReqDto params) {
        Food food = new Food();
        BeanUtils.copyProperties(params,food);
        return foodMapper.addFood(food);
    }

    @Override
    public Food getFoodByFoodId(Integer foodId) {
        return foodMapper.getFoodById(foodId);
    }

    @Override
    public Integer updateFood(FoodReqDto params) {
        Food food = new Food();
        BeanUtils.copyProperties(params,food);
        return foodMapper.updateFoodById(food);
    }

    @Override
    public Integer deleteFood(Integer foodId) {
        return foodMapper.delFood(foodId);
    }

    @Override
    public List<Food> listFoodByBusinessId(Integer businessId) {
        return foodMapper.listFoodByBusinessId(businessId);
    }
}
